package edu.arizona.arid.akshen.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;

import edu.arizona.arid.akshen.util.HttpMultipartRequest;
import edu.arizona.arid.akshen.view.ImageView;

public final class PictureUploadController extends ViewController implements
		CommandListener {

	private ChoiceGroup radioButtons;
	private FileConnection imageFileConnection;
	private String imageFilePath;
	private Image image;

	public PictureUploadController(String filePath) {
		imageFilePath = filePath;
	}
	
	public void viewWillAppear() {
		form = new Form("Akshen!");
		form.append("Loading image...");
		
		radioButtons = new ChoiceGroup("Make your choice", Choice.EXCLUSIVE);
		radioButtons.append("Liked it", null);
		radioButtons.append("Didn't like it", null);
		form.append(radioButtons);
		
		form.addCommand(new Command("Cancel", Command.CANCEL, 1));
		form.setCommandListener(this);
	}
	
	public void viewDidAppear() {
		InputStream in = null;
		try {
			imageFileConnection = (FileConnection) Connector.open(imageFilePath);
			in = imageFileConnection.openInputStream();
			image = Image.createImage(in);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		image = ImageView.resizeImage(image, form.getWidth(), 160);
		ImageItem imageItem = new ImageItem("Image", image, ImageItem.LAYOUT_DEFAULT, "image");
		form.set(0, imageItem);
		
		form.addCommand(new Command("Submit", Command.OK, 1));
	}

	public void commandAction(Command cmd, Displayable displayable) {
		System.out.println("commandAction, " + cmd.getCommandType());

		switch (cmd.getCommandType()) {
		case Command.OK:
			uploadImage();
			break;

		case Command.CANCEL:
			getNavigationController().popViewController();
			break;

		default:
			// throw new Exception("Unknown command type");
		}
	}

	private void uploadImage() {
		Hashtable param = new Hashtable();
		param.put("filename", imageFilePath);
		param.put("width", String.valueOf(image.getWidth()));

		InputStream in = null;
		try {
			in = imageFileConnection.openInputStream();
			HttpMultipartRequest req = new HttpMultipartRequest(
					"http://home.sumin.us/labs/recall/post.php", param,
					"upload", imageFilePath, "image/jpg", in);
			byte[] response = req.send();

			System.out.println("Response:");
			System.out.println(new String(response));

			// TODO: Need to define a response protocol
			uploadSucceeded();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void uploadSucceeded() {
		Alert alert = new Alert("Alert", "Image was uploaded successfully.",
				null, AlertType.INFO);
		alert.setTimeout(3000);
		alert.addCommand(new Command("Dismiss", Command.OK, 0));
		alert.setCommandListener(new CommandListener() {
			public void commandAction(Command c, Displayable s) {
				navigationController.popViewController();
			}
		});
		navigationController.displayAlert(alert);
	}

	private void uploadFailed() {
		Alert alert = new Alert("Alert", "Image could not be uploaded.",
				null, AlertType.INFO);
		alert.setTimeout(3000);
		alert.addCommand(new Command("Dismiss", Command.OK, 0));
	}

}
